# application/api/detection/product_coil.py
from flask import Blueprint, request
from application.services.detection.product_coil import ProductCoilService
from application.utils.common import parse_id_list
from application.utils.res import json_success, json_fail, page_success

# 创建蓝图
bp = Blueprint("product_coil", __name__, url_prefix="/product/coil")


@bp.get("/list")
def get_product_coil_list():
    """
    分页获取绕组列表
    """
    try:
        # 获取分页参数
        page_num = int(request.args.get("pageNum", 1))
        page_size = int(request.args.get("pageSize", 10))
        
        # 获取查询条件
        conditions = {
            "coil": request.args.get("coil"),
            "productModelId": request.args.get("productModelId"),
            "coilAccuracyLevel": request.args.get("coilAccuracyLevel"),
            "coilType": request.args.get("coilType")
        }
        
        # 过滤空值条件
        filtered_conditions = {
            k: v for k, v in conditions.items() if v is not None and v != ""
        }
        
        # 调用服务获取数据
        coil_list, total = ProductCoilService.get_product_coil_list(
            page_num, page_size, filtered_conditions
        )
        
        return page_success(coil_list, total)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500


@bp.post("")
def create_product_coil():
    """
    创建绕组
    """
    try:
        # 获取请求数据
        data = request.get_json()
        
        if not data:
            return json_fail("请求数据不能为空"), 400
            
        result = ProductCoilService.create_product_coil(data)
        return json_success(result, "创建成功")
    except Exception as e:
        return json_fail(f"创建失败: {str(e)}"), 500


@bp.put("")
def update_product_coil():
    """
    更新绕组
    """
    try:
        # 获取请求数据
        data = request.get_json()
        
        if not data:
            return json_fail("请求数据不能为空"), 400
            
        coil_id = data.get("coilId") or data.get("id")
        if not coil_id:
            return json_fail("绕组ID不能为空"), 400
            
        result = ProductCoilService.update_product_coil(coil_id, data)
        if result:
            return json_success(result, "更新成功")
        else:
            return json_fail("绕组不存在"), 404
    except Exception as e:
        return json_fail(f"更新失败: {str(e)}"), 500


@bp.delete("/<string:coil_ids>")
def batch_delete_product_coil(coil_ids):
    """
    删除绕组
    :param coil_id: 绕组ID
    """
    try:
        id_list = parse_id_list(coil_ids)

        count = ProductCoilService.batch_delete_product_coil(id_list)
      
        return json_success(count, "删除成功")
       
    except Exception as e:
        return json_fail(f"删除失败: {str(e)}"), 500


@bp.get("/<int:coil_id>")
def get_product_coil_detail(coil_id):
    """
    获取绕组详情
    :param coil_id: 绕组ID
    """
    try:
        result = ProductCoilService.get_product_coil_by_id(coil_id)
        if result:
            return json_success(result)
        else:
            return json_fail("绕组不存在"), 404
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500


@bp.get("/by-product-model/<int:product_model_id>")
def get_coils_by_product_model(product_model_id):
    """
    根据产品型号ID获取绕组列表
    :param product_model_id: 产品型号ID
    """
    try:
        result = ProductCoilService.get_coils_by_product_model(product_model_id)
        return json_success(result)
    except Exception as e:
        return json_fail(f"查询失败: {str(e)}"), 500